<template>
  <el-dialog
    title="新增交办单"
    :visible.sync="show"
    :close-on-click-modal="false"
    width="700px"
    @close="closeDialog"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="120px"
      :inline="false"
    >
      <el-form-item label="问题描述" prop="problem_desc">
        <el-input
          type="textarea"
          :autosize="{
            minRows: 3,
            maxRows: 6,
          }"
          v-model="form.problem_desc"
          placeholder=""
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="承办单位" prop="undertake_dept_id">
        <DeptCascader
          :options="dept"
          :defaultChecked.sync="form.undertake_dept_id"
          @change="changeDept"
        >
        </DeptCascader>
      </el-form-item>
      <el-form-item label="交办意见" prop="assign_opinion">
        <el-input
          type="textarea"
          :autosize="{
            minRows: 3,
            maxRows: 6,
          }"
          v-model="form.assign_opinion"
          placeholder=""
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="反馈截止日期" prop="feedback_date_require">
        <el-date-picker
          v-model="form.feedback_date_require"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期时间"
          :picker-options="{
            disabledDate: (c) =>
              c.getTime() < currentDate.getTime() - 1000 * 60 * 60 * 24,
          }"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="交办日期" prop="">
        <el-date-picker
          v-model="currentDate"
          disabled
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="save">交办</el-button>
        <el-button type="" @click="closeDialog">返回</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import DeptCascader from "@/components/Common/DeptCascader.vue";
import { getDeptTree } from "@/api/System/Department";
import { required } from "@/utils/eValidate";
import { addInstructionsAssign } from "@/api/LeaderInstructions/Index";
import { postAssignSave } from "@/api/MajorProjectPromotion/Index";
export default {
  components: { DeptCascader },
  props: {
    current: {
      type: Object,
      default: null,
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      currentDate: new Date(),
      form: {
        problem_desc: "",
        undertake_dept_id: "",
        undertake_dept_name: "",
        assign_opinion: "",
        feedback_date_require: "",
      },
      other: {
        source_id: "",
        project_id: "",
      },
      rules: {
        problem_desc: [required],
        undertake_dept_id: [required],
        assign_opinion: [required],
        feedback_date_require: [required],
      },
      dept: [],
    };
  },
  methods: {
    save() {
      this.$refs.form.validate().then((valid) => {
        postAssignSave(Object.assign(this.form, this.other)).then((res) => {
          if (res.code == 0) {
            this.$message.success("交办成功");
            this.$emit("update");
            this.closeDialog();
          }
        });
      });
    },
    closeDialog() {
      this.$refs.form.resetFields();
      this.$emit("update:visible", false);
    },
    initDept() {
      getDeptTree().then((res) => {
        if (res.code == 0) {
          this.dept = res.data;
        }
      });
    },
    changeDept(val) {
      this.form.undertake_dept_id = val.value;
      this.form.undertake_dept_name = val.label;
    },
  },
  created() {
    this.initDept();
  },
  computed: {
    show: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
  },
  watch: {
    current(newVal, oldVal) {
      if (newVal) {
        this.other.project_id = newVal.project_id;
        this.other.source_id = newVal.id;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
</style>